package com.ht.impl.drawtool;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;

import javax.swing.*;
import java.awt.event.MouseEvent;

/**
 * a shape class for drawing a line.
 * <p>provider methods to draw a line.<br>
 *
 * @author Dong Shufeng
 * @version 1.0
 */
public class LineTool extends DrawTool {
    public LineTool(String style) {
        super(style);
    }

    //implemented methods

    public void mousePressed(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)) {
            mxGeometry geometry = new mxGeometry(0, 0, 0, 0);
            geometry.setRelative(true);

            cell = new mxCell(null, geometry, style);
            cell.setEdge(true);
            startPoint = e.getPoint();
            double factor = graphComponent.getGraph().getView().getScale();
            mxPoint offset = graphComponent.getGraph().getView().getTranslate();
            cell.getGeometry().setSourcePoint(new mxPoint(e.getX() / factor - offset.getX(), e.getY() / factor - offset.getY()));
        }
    }

    public void mouseReleased(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)) {
            if (e.getPoint().distance(startPoint) < 3) {
                graphComponent.getGraph().getModel().remove(cell);
                return;
            }
            double factor = graphComponent.getGraph().getView().getScale();
            mxPoint offset = graphComponent.getGraph().getView().getTranslate();
            endPoint = e.getPoint();
            cell.getGeometry().setTargetPoint(new mxPoint(endPoint.getX() / factor - offset.getX(), endPoint.getY() / factor - offset.getY()));
            updateGraphElement();
            graphComponent.getGraph().setCellStyles(mxConstants.STYLE_ENDARROW, mxConstants.NONE, new Object[]{cell});
            graphComponent.getGraph().setCellStyles(mxConstants.STYLE_STARTARROW, mxConstants.NONE, new Object[]{cell});
        }
    }

    public void mouseDragged(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)) {
            int i1 = e.getX() / xStep;
            if (e.getX() % xStep > xStep / 2)
                i1++;
            int i2 = e.getY() / yStep;
            if (e.getY() % yStep > yStep / 2)
                i2++;
            //oval2.x = xStep * i1 - oval1.width / 2.0;
            //oval2.y = xStep * i2 - oval1.height / 2.0;
            assistPoint.setLocation(xStep * i1, yStep * i2);
            double factor = graphComponent.getGraph().getView().getScale();
            mxPoint offset = graphComponent.getGraph().getView().getTranslate();

            cell.getGeometry().setTargetPoint(new mxPoint(e.getPoint().getX() / factor - offset.getX(), e.getPoint().getY() / factor - offset.getY()));
            updateGraphElement();
            graphComponent.getGraph().setCellStyles(mxConstants.STYLE_ENDARROW, mxConstants.NONE, new Object[]{cell});
            graphComponent.getGraph().setCellStyles(mxConstants.STYLE_STARTARROW, mxConstants.NONE, new Object[]{cell});
        }
    }
}

